﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using FastReflectionLib;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.QY.Entities;
using SZHL.Data;
using SZHL.UI.AppCode;

namespace SZHL.UI.API
{
    public class RCTXManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(RCTXManage).GetMethod(msg.Action.ToUpper());
            RCTXManage model = new RCTXManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }

        /// <summary>
        /// 添加请假数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="strParamData"></param>
        public void ADDRCTX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                ZH_RCTX rctx = JsonConvert.DeserializeObject<ZH_RCTX>(P1);
                if (rctx.RWTitle.Trim() == "")
                {
                    msg.ErrorMsg = "任务内容不能为空";
                    return;
                }
                if (rctx.ID == 0)
                {
                    rctx.CRDate = DateTime.Now;
                    rctx.CRUser = UserInfo.User.UserName;
                    new ZH_RCTXB().Insert(rctx);
                }
                else
                {
                    new ZH_RCTXB().Update(rctx);
                }
                msg.Result = rctx;

            }
            catch (Exception)
            {

                msg.ErrorMsg = "添加待办失败";
            }
        }
        /// <summary>
        /// 查看任务代办的详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETRCTXMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strFormID = P1;
            ZH_RCTX Model = new ZH_RCTXB().GetEntity(d => d.ID.ToString() == strFormID);
            msg.Result = Model;
            msg.Result1 = new JH_Auth_TLB().GetTL("RCTX", Model.ID.ToString());


        }
        public void GETRCTXDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " Where 1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And RWStatus={0}", P1);
            }
            if (P2 != "")
            {

                switch (P2)
                {
                    case "0":
                        strWhere += string.Format(" And (CRUser='{0}' or  ','+RWFZR+','  like '%,{0},%'  or ','+RWCYR+','  like '%,{0},%' )", UserInfo.User.UserName);
                        break;
                    case "1":
                        strWhere += string.Format(" And CRUser='{0}'", UserInfo.User.UserName);
                        break;
                    case "2":
                        strWhere += string.Format(" And (','+RWFZR+','  like '%," + UserInfo.User.UserName + ",%' )");
                        break;
                    case "3":
                        strWhere += string.Format(" And (','+RWCYR+','  like '%," + UserInfo.User.UserName + ",%' )");
                        break;
                }
            }
            DataTable dtList = new DataTable();
            dtList = new ZH_RCTXB().GetDTByCommand(" select *  FROM ZH_RCTX " + strWhere + " ORDER BY LeiBie DESC,CRDate DESC ");
            dtList.Columns.Add("CRUserName");
            dtList.Columns.Add("PLList", Type.GetType("System.Object"));
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                dtList.Rows[i]["PLList"] = new JH_Auth_TLB().GetTL("RCTX", dtList.Rows[i]["ID"].ToString());
                dtList.Rows[i]["CRUserName"] = new JH_Auth_UserB().GetUserRealName(dtList.Rows[i]["CRUser"].ToString());

            }
            msg.Result = dtList;

        }


        public void DELRWDB(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            new ZH_RCTXB().Delete(D => D.ID.ToString() == P1);
        }
       
        public void COMPLETERCTX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            //如果提醒状态的评论带结束标志，更新待办的状态
            new ZH_RCTXB().ExsSql("UPDATE [dbo].[ZH_RCTX] SET RWStatus='" + P2 + "' WHERE ID='" + P1 + "'");//
            msg.Result = "";
        }



    }


}
